﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

using WlanDesign;

namespace WlanDesignDll
{
    public partial class FormLayerSet : Form
    {
        private static Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        private static Database acCurDb = acDoc.Database;
        public FormLayerSet()
        {
            InitializeComponent();
        }

        private void FormLayerSet_Load(object sender, EventArgs e)
        {
            this.cBox_Building.DataSource = IterateLayers();
            this.lBox_Antenna.DataSource = IterateLayers();
            this.cBox_Building.SelectedIndex = -1;
            this.lBox_Antenna.SelectedIndex = -1;
        }
        //
        // Summery
        // 获得所有图层
        //
        public static List<string> IterateLayers()
        {
            // Get the current document and database, and start a transaction
            List<string> name = new List<string>();

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // This example returns the layer table for the current database
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                             OpenMode.ForRead) as LayerTable;

                // Step through the Layer table and print each layer name              
                foreach (ObjectId acObjId in acLyrTbl)
                {
                    LayerTableRecord acLyrTblRec;
                    acLyrTblRec = acTrans.GetObject(acObjId,
                                                    OpenMode.ForRead) as LayerTableRecord;

                    name.Add(acLyrTblRec.Name);
                }

                // Dispose of the transaction
            }
            return name;
        }

        //
        // Summery
        // 加载信息到ini文件
        //
        public void WriteIniTxt(string sPath, string Section, string Key, string value)
        {
            string ServerName = value;

            IniFileRW ini = new IniFileRW(sPath);

            ini.IniWriteValue(Section, Key, ServerName);
        }

        //“保存”按钮
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.cBox_Building.SelectedIndex < 0)
            {
                MessageBox.Show("请选择建筑物图层！");
                return;
            }
            else if (this.lBox_Antenna.SelectedIndex < 0)
            {
                MessageBox.Show("请选择天线图层！");
                return;
            }
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";
            // 获得建筑物图层的选项
            string buildLyr = this.cBox_Building.SelectedItem.ToString();
            // 获得天线图层的选项，做成“ZN-天线,ZN-功分器,…”字符串
            string antLyr = null;

            for (int i = 0; i < this.lBox_Antenna.SelectedItems.Count - 1; i++)
            {
                antLyr += this.lBox_Antenna.SelectedItems[i].ToString() + ",";
            }

            antLyr += this.lBox_Antenna.SelectedItems[this.lBox_Antenna.SelectedItems.Count - 1]
                .ToString();
            // 写入ini文件
            WriteIniTxt(iniName, "layer", "building_layer", buildLyr);
            WriteIniTxt(iniName, "layer", "antenna_layer", antLyr);
            MessageBox.Show("保存成功！");
            this.Close();
        }

        //“取消”按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.cBox_Building.SelectedIndex = -1;
            this.lBox_Antenna.SelectedIndex = -1;
        }
    }
}
